package com.mm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mm.constant.BusinessConstant;
import com.mm.dao.CompanyMapper;
import com.mm.exception.ServiceException;
import com.mm.model.Company;
import com.mm.model.sys.JsonResponse;
import com.mm.service.CompanyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 店铺service实现类
 *
 * @date 2018/7/18 19:36
 */
@Service(value = "companyService")
public class CompanyServiceImpl implements CompanyService {
	@Autowired
	private CompanyMapper companyMapper;

	/**
	 * 保存店铺
	 *
	 * @param jsonData String(JSON格式字符串)
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void saveCompany(String jsonData) throws ServiceException {
		if (StringUtils.isBlank(jsonData)) {
			throw new ServiceException("输入参数不能为空");
		}
		JSONObject dataJsonObj = JSON.parseObject(jsonData);
		//获取店铺资料
		String companyJsonString = dataJsonObj.getString(BusinessConstant.JSON_KEY_COMPANY);
		if (StringUtils.isNotBlank(companyJsonString)) {
			//转化为Company对象
			Company company = JSON.parseObject(companyJsonString, Company.class);
			//保存Company
			companyMapper.insertCompany(company);
			//获取店铺图片关系字符串
			String pictureIdString = dataJsonObj.getString(BusinessConstant.JSON_KEY_PICTURE_IDS);
			if (StringUtils.isNotBlank(pictureIdString)) {
				//保存店铺图片关系
				saveCompanyPicture(company.getCompanyId(), pictureIdString);
			}
		}
	}

	/**
	 * 更新店铺
	 *
	 * @param company Company
	 * @throws ServiceException
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void updateCompany(Company company) {
		if (company != null) {
			companyMapper.updateCompanyById(company);
		}
	}

	/**
	 * 获取店铺
	 *
	 * @param companyId 店铺ID
	 * @return Company
	 * @throws ServiceException
	 */
	@Override
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public Company getOneCompanyById(Long companyId) throws ServiceException {
		if (companyId == null) {
			throw new ServiceException("店铺ID不能为空");
		}
		return companyMapper.getOneCompanyById(companyId);
	}

	/**
	 * 通过店铺ID删除店铺
	 *
	 * @param companyId 店铺ID
	 * @throws ServiceException
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void deleteCompanyById(Long companyId) throws ServiceException {
		if (companyId == null) {
			throw new ServiceException("店铺ID不能为空");
		}
		companyMapper.deleteCompanyById(companyId);
	}

	/**
	 * 保存店铺图片关系
	 *
	 * @param companyId  店铺ID
	 * @param pictureIds 图片ID字符串
	 * @throws ServiceException
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void saveCompanyPicture(Long companyId, String pictureIds) throws ServiceException {
		if (companyId == null || StringUtils.isBlank(pictureIds)) {
			throw new ServiceException("输入参数不能为空");
		}
		String[] pictureIdArray = pictureIds.split(",");
		companyMapper.insertCompanyPicture(companyId, pictureIdArray);
	}
}
